#!/bin/ksh
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2022 Tintri by DDN, Inc.  All rights reserved.
#

# Use normal make (not dmake) by default.
make=${MAKE:-make}

CLOSED_IS_PRESENT=no
export CLOSED_IS_PRESENT

# Do this if you want to use dbx or gdb
# export SOURCEDEBUG=yes

[ -n "$SRC" ] || {
  echo "SRC not set.  Run 'ws' or 'bldenv' first."
  exit 1
}

cpu=`uname -p`
case $cpu in
i386)
	x=intel
	kmdb_arch="amd64"
	mdb_arch="ia32 amd64"
	arch64=amd64
	;;
sparc)
	x=sparc
	kmdb_arch=v9
	mdb_arch="v7 v9"
	arch64=sparcv9
	;;
*)  echo "Huh?" ; exit 1;;
esac

################################################################

build_tools() {
  test -f $SRC/tools/proto/root_i386-nd/opt/onbld/bin/genoffsets ||
    (cd $SRC/tools && $make install)
  (cd $SRC/common/mapfiles; $make install)
}

clobber_tools() {
  (cd $SRC/tools && $make clobber)
  (cd $SRC/common/mapfiles; $make clobber)
}

################################################################

do_hdrs() {

targ=$1
if [ "$targ" = clobber ]
then
  (cd $SRC/uts && $make -k clobber_h)
  (cd $SRC/head && $make clobber)
fi

if [ "$targ" = install ]
then
  targ=install_h

  # Just the parts of "make sgs" we need, and
  # skip them if they appear to be done.
  # ... stuff under $SRC
  test -f $SRC/uts/common/sys/priv_names.h ||
    (cd $SRC/uts && $make -k all_h)

  test -f $SRC/head/rpcsvc/nispasswd.h ||
    (cd $SRC/head && $make -k $targ)

  # ... stuff under $ROOT (proto area)
  test -d $ROOT/usr/include/sys ||
    (cd $SRC && $make rootdirs)
  test -f $ROOT/usr/include/sys/types.h ||
    (cd $SRC/uts && $make -k $targ)
  test -f $ROOT/usr/include/rpcsvc/daemon_utils.h ||
    (cd $SRC/head && $make $targ)

  # always update the sys,smbsrv headers to be safe
  (cd $SRC/uts/common/gssapi && $make -k $targ)
  (cd $SRC/uts/common/sys && $make -k $targ)
  (cd $SRC/uts/common/smb && $make -k $targ)
  (cd $SRC/uts/common/smbsrv && $make -k $targ)
  (cd $SRC/uts/common/c2 && $make -k $targ)
fi

if [ "$targ" = lint ]
then
  targ=check
  (cd $SRC/uts/common/smb && $make -k $targ)
  (cd $SRC/uts/common/smbsrv && $make -k $targ)
fi

# Need some library headers too...
for lib in \
  libc \
  libnsl \
  libnvpair \
  libsocket \
  \
  libads \
  libbrand \
  libbsm \
  libcmdutils \
  libcryptoutil \
  libdevid \
  libdisasm \
  libfakekernel	\
  libgss \
  libidmap \
  libinetutil \
  libipsecutil \
  libkrb5 \
  libmlrpc \
  libpam \
  libsaveargs \
  libsec \
  libscf \
  libshare \
  libsmbfs \
  libsqlite \
  libuutil \
  libzfs_core \
  libzfs \
  passwdutil \
  pkcs11 \
  smbsrv
do
  (cd $SRC/lib/$lib && $make $targ)
done
}

################################################################

do_kern() {
  case $1 in
  lint) targ=modlintlib ;;
  *) targ=$1 ;;
  esac
  ( unset SOURCEDEBUG ;
   export NO_GENUNIX_UNIQUIFY= ;
  (cd $SRC/uts/$x/smbsrv && $make $targ) )
}

################################################################
#
# Build all libraries used by the other targets in here.
#
# Run this once (at least) in each new workspace where you
# will run "make-smbsrv install", if you want to avoid linking
# against the libraries from your build host.
#
do_deplibs() {

(cd $SRC/lib/ssp_ns && $make $1)
(cd $SRC/lib/libc && $make $1)

for lib in \
  libm \
  libmd \
  libnsl \
  libnvpair \
  libsocket \
  libavl \
  libgss \
  libgen \
  libkrb5 \
  libkstat \
  libcmdutils \
  libresolv2 \
  libldap5 \
  libsldap \
  libreparse \
  libpam \
  libuutil \
  libidmap \
  libinetutil \
  libdlpi \
  libbsm \
  libsec \
  libsecdb \
  libsqlite \
  libumem \
  libuuid \
  libsaveargs \
  libproc \
  libscf \
  libcryptoutil \
  libmd5 \
  libzfs_core \
  libzfs \
  pkcs11/libpkcs11
do
  # So we don't have to build EVERYTHING, set LDCHECKS=
  # when building the dependent libraries.
  (cd $SRC/lib/$lib && LDCHECKS='' $make $1)
done
}

################################################################

do_libs() {

for lib in \
  libfakekernel \
  libads \
  libidmap \
  libsmbfs \
  libmlrpc
do
  (cd $SRC/lib/$lib && $make $1)
done

(cd $SRC/lib/libshare && $make $1 PLUGINS=smb)
(cd $SRC/lib/smbsrv && $make $1)
(cd $SRC/lib/passwdutil && $make $1)
(cd $SRC/lib/pam_modules/smb && $make $1)

}

################################################################

do_cmds() {

(cd $SRC/cmd/smbsrv && $make $1)

# Build the MDB modules, WITH the linktest
(cd $SRC/cmd/mdb/tools && $make $1)

# kmdb_arch is 64-bit only
for a in $kmdb_arch
do
  case $1 in
  install|lint)
    (cd $SRC/cmd/mdb/$x/$a/kmdb &&
	$make kmdb_modlinktest.o )
    ;;
  clean|clobber)
    (cd $SRC/cmd/mdb/$x/$a/kmdb &&
	$make -k $1 )
    ;;
  esac
  (cd $SRC/cmd/mdb/$x/$a/smbsrv &&
	$make $1 KMDB_LINKTEST_ENABLE= )
done

# mdb_arch is both 32-bit & 64-bit
for a in $mdb_arch
do
  # We build these libraries (to the proto area), so we need to
  # build the mdb modules for all dependent libraries too.

  for lib in libfksmbsrv libmlsvc list libavl
  do
    (cd $SRC/cmd/mdb/$x/$a/$lib && $make $1 )

  done
done
}

################################################################

do_tests() {

  for d in test/test-runner test/libmlrpc-tests test/smbsrv-tests
  do
       [ -f $SRC/$d/Makefile ] && (cd $SRC/$d && $make $1)
  done
}

################################################################
# This builds $SRC/TAGS (and cscope.files) in a helpful order.

do_tags() {
	(cd $SRC ;
	find uts/common/sys -name '*.[ch]' -print |sort
	find uts/common/net -name '*.[ch]' -print |sort
	find uts/common/netinet -name '*.[ch]' -print |sort
	find uts/common/smb -name '*.[ch]' -print |sort
	find uts/common/smbsrv -name '*.ndl' -print |sort
	find uts/common/smbsrv -name '*.[ch]' -print |sort
	find uts/common/fs/smbsrv -name '*.[ch]' -print |sort
	find uts/common/gssapi -name '*.[ch]' -print |sort
	find common/smbsrv -name '*.[ch]' -print |sort
	find head -name '*.h' -print |sort
	find lib/smbsrv -name '*.[ch]' -print |sort
	find lib/libsmbfs -name '*.[ch]' -print |sort
	find lib/libmlrpc -name '*.ndl' -print |sort
	find lib/libmlrpc -name '*.[ch]' -print |sort
	find lib/libads -name '*.[ch]' -print |sort
	find lib/libgss -name '*.[ch]' -print |sort
	find cmd/smbsrv -name '*.[ch]' -print |sort
	) > $SRC/cscope.files

	(cd $SRC ;
	exctags -e --langmap=c:+.ndl -h ndl -L - < cscope.files
	cscope -b )
}

################################################################
# This creates a tarfile one can use to update a test machine.

do_tar() {
	git_rev=`git rev-parse --short=8 HEAD`
	files="
lib/svc/manifest/network/smb/server.xml
usr/kernel/drv/$arch64/smbsrv
usr/kernel/kmdb/$arch64/smbsrv
usr/lib/fs/smb/$arch64/libshare_smb.so.1
usr/lib/fs/smb/libshare_smb.so.1
usr/lib/libsmbfs.so.1
usr/lib/mdb/kvm/$arch64/smbsrv.so
usr/lib/mdb/proc/libmlsvc.so
usr/lib/reparse/libreparse_smb.so.1
usr/lib/security/pam_smb_passwd.so.1
usr/lib/smbsrv/dtrace
usr/lib/libmlrpc.so.2
usr/lib/smbsrv/libmlsvc.so.1
usr/lib/smbsrv/libsmb.so.1
usr/lib/smbsrv/libsmbns.so.1
usr/lib/smbsrv/nvlprint
usr/lib/smbsrv/smbd
usr/sbin/smbadm
usr/sbin/smbstat
"
	(cd $ROOT && tar cfj ../../smbsrv-${git_rev}.tar.bz2 $files)
}

################################################################

if [ "$1" = "" ]; then
  set '?' # force usage
fi

set -x

for arg
do
  case "$arg" in
  install)
    build_tools
    set -e
    do_hdrs $arg
    do_kern $arg
    do_libs $arg
    do_cmds $arg
    do_tests $arg
    ;;
  lint)
    do_hdrs $arg
    do_kern $arg
    do_libs $arg
    do_cmds $arg
    ;;
  clean)
    # intentionally skip: deplib, hdrs, tools
    do_tests $arg
    do_cmds $arg
    do_libs $arg
    do_kern $arg
    ;;
  clobber)
    do_tests $arg
    do_cmds $arg
    do_libs $arg
    do_deplibs $arg
    do_kern $arg
    do_hdrs $arg
    clobber_tools
    ;;
  deplibs)
    build_tools
    set -e
    do_hdrs install
    do_deplibs install
    ;;
  tags)
    do_tags
    ;;
  tar)
    do_tar
    ;;
  *)
    echo "Usage: $0 {install|lint|clean|clobber|deplibs|tags|tar}";
    exit 1;
    ;;
  esac
done
